package com.example.myapplication10;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class BookAdapter extends ArrayAdapter<Book> {
    private Context context;
    private List<Book> books;

    public BookAdapter(Context context, List<Book> books) {
        super(context, 0, books);
        this.context = context;
        this.books = books;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Book book = books.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_book, parent, false);
        }

        ImageView imgCover = convertView.findViewById(R.id.imgCover);
        TextView txtTitle = convertView.findViewById(R.id.txtTitle);
        TextView txtAuthor = convertView.findViewById(R.id.txtAuthor);

        imgCover.setImageResource(book.getCoverResId());
        txtTitle.setText(book.getTitle());
        txtAuthor.setText(book.getAuthor());
        // 点击事件：传递完整数据到详情页
        convertView.setOnClickListener(v -> {
            Intent intent = new Intent(context, BookDetailActivity.class);
            intent.putExtra("title", book.getTitle());
            intent.putExtra("author", book.getAuthor());
            intent.putExtra("coverResId", book.getCoverResId());
            intent.putExtra("desc", book.getDesc()); // 确保传递简介
            context.startActivity(intent);
        });

        return convertView;
    }
}